package com.hctforgreen.greenservice.model;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.hctforgreen.greenservice.utils.HctConstants;

/**
 * 6.17、我的收藏夹查询
 * @author Tallen Zhai
 *
 */
public class ColListEntity extends BaseEntity {

	private static final long serialVersionUID = 6888672015200124062L;

	public List<ColEntity> dataList = null;
	
	public String state = "";
	
	public final static String STATE = "state";
	private final static String BODY = "body";
	
	public static ColListEntity parse(JSONObject jsonObject)throws JSONException{
		ColListEntity bean = new ColListEntity();
		if(jsonObject.has(STATE)){
			bean.state = jsonObject.getString(STATE);
		}
		/**
		 * 解析body节点
		 */
		if (jsonObject.has(BODY)) {
			
			JSONArray jsonArray = jsonObject.getJSONArray(BODY);
				
			bean.dataList = new ArrayList<ColEntity>();
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject object = jsonArray.getJSONObject(i);
				ColEntity entity = ColEntity.parse(object);
				bean.dataList.add(entity);
			}
			
		}
		/**
		 * -1表示:无数据
		 */
		if(bean.state.equals(HctConstants.RESULT_STATE_FAILD)
				&& bean.dataList == null){
			bean.dataList = new ArrayList<ColEntity>();
		}
		return bean;
	}
	
	public static class ColEntity extends BaseEntity {

		private static final long serialVersionUID = -8858468913114108483L;
		public String contextId = "";
		public final static String CONTEXT_ID = "contextId";
		public static ColEntity parse(JSONObject jsonObject)
				throws JSONException {
			ColEntity bean = new ColEntity();
			if (jsonObject.has(CONTEXT_ID)) {
				bean.contextId = jsonObject.getString(CONTEXT_ID);
			}
			
			return bean;
		}
	}
}
